package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工管理模块")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        //获取用户名
        String username = employee.getUsername();
        //根据用户名查询
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Employee> wrapper = queryWrapper.eq("username", username);//用户名条件
        Employee emp = employeeService.getOne(wrapper);
        //对用户名进行判断
        if(emp==null){
            return R.error("用户名错误");
        }
        // 比较两个密码 数据库密码和提交的密码
        String pwd1 = emp.getPassword();
        String pwd2 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if(!pwd1.equals(pwd2)){
            return R.error("密码错误");
        }
        //获取员工状态信息 进行判断
        Integer status = emp.getStatus();
        if(status == 0){
            return R.error("员工账号已禁用");
        }
        //登录成功 响应code=1 data =emp 将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }
//添加员工
    @PostMapping
    @ApiOperation("添加员工")
    public R<String> add(@RequestBody Employee employee,HttpServletRequest request){
        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置添加时间和修改时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //设置添加用户和修改用户     获得当前登录用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加成功");
    }

//分页条件查询
    @GetMapping("/page")
    public R<Page<Employee>> page(String name,Integer page, Integer pageSize){
        //构造分页对象
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        //构造条件查询
        QueryWrapper<Employee> queryWrapper  =new QueryWrapper<>();
        //判断name是否为空 如果不为空才会有值
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        //根据时间进行排序
        queryWrapper.orderByDesc("update_time");

        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

// 根据id修改员工信息
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        //设置更新时间
//        employee.setUpdateTime(LocalDateTime.now());
        //设置更新id
//        employee.setUpdateUser((Long) session.getAttribute("employee"));
        //调用修改方法
        employeeService.updateById(employee);
        return  R.success("更新成功");
    }


//根据id查询员工信息
    @GetMapping("{id}")
    public R<Employee> findById(@PathVariable Long id ){
        //根据id获取对象所有值
        Employee employee = employeeService.getById(id);
        //返回 回显数据
        return R.success(employee);
    }
}
